package top.tangyh.basic.spring_mvc.controller.system;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.tangyh.basic.spring_mvc.base.R;
import top.tangyh.basic.spring_mvc.controller.base.SuperController;
import top.tangyh.basic.spring_mvc.util.TreeUtil;
import top.tangyh.basic.spring_mvc.entity.BaseOrg;
import top.tangyh.basic.spring_mvc.service.system.BaseOrgService;
import top.tangyh.basic.spring_mvc.vo.query.system.BaseOrgPageQuery;
import top.tangyh.basic.spring_mvc.vo.result.system.BaseOrgResultVO;
import top.tangyh.basic.spring_mvc.vo.save.system.BaseOrgSaveVO;
import top.tangyh.basic.spring_mvc.vo.update.system.BaseOrgUpdateVO;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 组织
 * </p>
 *
 * @author CJ
 * @date 2021-10-18
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseOrg")
public class BaseOrgController extends SuperController<BaseOrgService, Long, BaseOrg, BaseOrgSaveVO, BaseOrgUpdateVO, BaseOrgPageQuery, BaseOrgResultVO> {

    /**
     * 部门增加Add重载父类方法
     * 实现：1-检查部门(整棵树)名称是否重复,由BaseOrgServiceImpl类check方法检测 2-默认设置部门系列属性值
     * @param model
     * @return R<基础部门类对象></>
     */
    @Override
    public R<BaseOrg> handlerSave(@Validated BaseOrgSaveVO model) {
        model.setType("20");
        model.setShortName("");
        model.setSortValue(1);
        model.setRemarks("");
        model.setState(true);
        return super.handlerSave(model);
    }

    /**
     * 部门编辑Edit重载父类方法
     * 实现：1-检查部门(整棵树)名称是否重复,由BaseOrgServiceImpl类check方法检测  2-默认设置部门系列属性值
     * @param model
     * @return R<基础部门类对象></>
     */
    @Override
    public R<BaseOrg> handlerUpdate(@Validated BaseOrgUpdateVO model) {
        model.setType("20");
        model.setShortName("");
        model.setSortValue(1);
        model.setRemarks("");
        return super.handlerUpdate(model);
    }


    @GetMapping("/check")
    public R<Boolean> check(@RequestParam String name, @RequestParam Long parentId, @RequestParam(required = false) Long id) {
        return success(superService.check(name, parentId, id));
    }

    /**
     * 按树结构查询地区
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    @PostMapping("/tree")
    public R<List<BaseOrgResultVO>> tree(@RequestBody BaseOrgPageQuery pageQuery) {
        List<BaseOrgResultVO> treeList = superService.list(pageQuery);
        return success(TreeUtil.buildTree(treeList));
    }

}
